/*
 +-----------------------------------------------------------------------------------------------+
 | Project : UKAPI
 +-----------------------------------------------------------------------------------------------+
 | Project Description : Universal & Kernel API
 +-----------------------------------------------------------------------------------------------+
 | File Description :
 |   Chained List
 |                                                 
 +-----------------------------------------------------------------------------------------------+
 | Licence : MIT Licence
 +-----------------------------------------------------------------------------------------------+
 | Copyright (c) 2006 Olivier Vanhoucke
 | 
 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 | and associated documentation files (the "Software"), to deal in the Software without 
 | restriction, including without limitation the rights to use, copy, modify, merge, publish, 
 | distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the 
 | Software is furnished to do so, subject to the following conditions:
 | 
 | The above copyright notice and this permission notice shall be included in all copies or 
 | substantial portions of the Software.
 | 
 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 +-----------------------------------------------------------------------------------------------+
*/

/**
 * Inclusions standards
 */
#include <ukapi.h>

/**
 * Inclusions locales
 */
#include <list.h>

/**
 * @fn		int list_merge( plist_t list, plist_t listtomerge[], ulong_t count, int type )
 * @brief	Fusionne plusieurs listes
 * @param	plist_t list : pointeur vers la structure de la liste
 * @param	plist_t listtomerge[] : pointeur vers un tableau de liste
 * @param	ulong_t count : taille du tableau
 * @param	int type : type de fusion
 * @return	Code erreur
 * @retval	0 : ok 
 * @retval	-1 : erreur
 */
result_t list_merge( plist_t list, plist_t listtomerge[], ulong_t count, list_merge_t type )
{
	register ulong_t	i	= 0;
	plist_node_t		le	= null;
	plist_node_t		le2	= null;

	if (	( list			== null ) 
		||	( listtomerge	== null )
		||	( count			== 0 )
		)
	{
		return result_invalidargument_e;
	}

	switch ( type )
	{
		case list_merge_after_e:
			for ( i = 0; i < count; i++ )
			{
				for ( le = list_head( listtomerge[i] ); le != null; le = list_next( le ) )
				{
					if ( list_insert_next( list, list_tail( list ), list_data( le ) ) != 0 )
					{
						return result_fail_e;
					}
				}
			}
			break;

		case list_merge_before_e:
			for ( i = 0; i < count; i++ )
			{
				for ( le = list_head( listtomerge[i] ); le != null; le = list_next( le ) )
				{
					if ( list_insert_next( list, null, list_data( le ) ) != 0 )
					{
						return result_fail_e;
					}
				}
			}
			break;

		case list_merge_into_e:
			for ( i = 0; i < count; i++ )
			{
				for	(	le = list_head( list ), le2 = list_head( listtomerge[i] ); 
						le2 != null; 
						le = list_next( list_next( le ) ), le2 = list_next( le2 ) 
					)
				{
					if ( list_insert_next( list, le, list_data( le2 ) ) != 0 )
					{
						return result_fail_e;
					}
				}
			}
			break;

		default: 
			return result_invalidargument_e;
	}

	return result_ok_e;
}
